
#include <boost/algorithm/string.hpp>
#include "WebKit.h"
#include "WebPage.h"
#include "WebElement.h"

#define SEARCE_BOX_CSS "#searchBoxUl > li"

using namespace SpiderWebKitClient;
using namespace std;
using namespace boost::algorithm;

void collectCtripTieyou(std::string protocol)
{
    WebKit* wk = new WebKit(protocol, WKMethodTypeStaticProtocol);
}


void collectCtripTieyou()
{
    WebKit* wk = new WebKit("test");

    /*
    ResFilter filter;
    filter.append("http://www.tieyou.com/*");
    wk->setFilter(filter);
    */

start:
    int error = 0;
    WebPage *newPage = wk->getWebPage(error, "http://www.tieyou.com/");

    usleep(200000);
    if (!newPage) {
        printf("get page error: %d\n", error);
        delete wk;
        return;
    }

    std::string method = newPage->method();
    std::string postData = newPage->postData();

    std::cout << method << " page url: " << newPage->getUrl() << " post data: " << (postData == "" ? "null" : postData) << std::endl;

    CookieJar jar1 = newPage->getCookieJar();
    std::cout << "cookie size: " << jar1.size() << std::endl;
    std::cout << jar1.toJson() << std::endl;

    std::list<WebElement> collection;
    collection = newPage->getElementsByCssSelector(".tool_05 > a:nth-child(1) > label:nth-child(1)");

    if (collection.size() < 0) {
        delete wk;
        return ;
    }

    WebElement element = collection.front();
    std::cout << "Get element " << element.getOuterXml() << std::endl;

    collection = newPage->getElementsByCssSelector(".hcp");

    if (collection.size() <= 0) {
        if (newPage->isFailed()) {
            std::cout << "goto start" << std::endl;
            goto start;
        }
    }

    element = collection.front();
    std::cout << "Click element " << element.getOuterXml() << std::endl;
    element.click();

    collection = newPage->getElementsByCssSelector("#train_from");

    if (collection.size() < 0)
        delete wk;

    element = collection.front();
    element.type("北京");
    std::cout << "input element " << element.getOuterXml() << std::endl;
    element.sendKeys("keypress", Key_Enter);

    collection = newPage->getElementsByCssSelector("#train_to");

    if (collection.size() < 0)
        delete wk;

    element = collection.front();
    element.type("太原");
    std::cout << "input element " << element.getOuterXml() << std::endl;
    element.sendKeys("keypress", Key_Enter);

    collection = newPage->getElementsByCssSelector("#wangdate");

    if (collection.size() < 0)
        delete wk;

    element = collection.front();
    std::cout << "data input element " << element.getOuterXml() << std::endl;
    element.click();

    collection = newPage->getElementsByCssSelector("div.pickerbody:nth-child(2) > div:nth-child(3) > ul:nth-child(1) > li:nth-child(25) > a:nth-child(1)");

    if (collection.size() < 0)
        delete wk;

    element = collection.front();
    std::cout << "data element " << element.getOuterXml() << std::endl;
    element.click();

    collection = newPage->getElementsByCssSelector("#form0 > input");

    if (collection.size() < 0)
        delete wk;

    element = collection.front();
    std::cout << "submit element " << element.getOuterXml() << std::endl;
    //element.sendKeys("keypress", Key_Tab);

    /*
     * test failded
     */
    std::cout << "sleep 10" << std::endl;
    sleep(10);

    error = newPage->click(element, 30000);

    if (error == NOTAVALIABLE_LOAD_ERROR) {
        if (newPage->isFailed()) {
            std::cout << "goto start" << std::endl;
            goto start;
        }
    }

    std::cout << method << " page url: " << newPage->getUrl() << " post data: " << (postData == "" ? "null" : postData) << std::endl;
    std::cout << "Get " << newPage->getTitle() << std::endl;

    CookieJar jar = newPage->getCookieJar();
    std::cout << "cookie size: " << jar.size() << std::endl;
    std::list<Cookie> cookies = jar.cookies();
    std::list<Cookie>::iterator it;
    for (it = cookies.begin(); it != cookies.end(); it++) {
        std::cout << (*it).toJson() << std::endl;
    }

    jar.set("__date__", "2014-09-25");

    std::string jarjson = jar.toJson();

    CookieJar nJar;
    nJar.loadWithJson(jarjson);

    newPage->setCookies(nJar.cookies());

    newPage->deleteCookie("__from__");


    CookieJar newjar = newPage->getCookieJar();
    std::list<Cookie> newcookies = newjar.cookies();
    for (it = newcookies.begin(); it != newcookies.end(); it++) {
        std::cout << (*it).toJson() << std::endl;
    }

    delete wk;
}

void collectCtrip()
{
    WebKit* wk = new WebKit("test");
    WebPage *page = wk->getWebPage("");
    usleep(200000);

 	std::map<std::string, std::string> header;

	header.insert(make_pair<std::string, std::string>("Cache-Control", "no-cache"));
	//page->setSetting(PAGE_SETTINGS_USER_AGENT_STRING, "GOME Spider Webkit Core v_1_0");

	page->loadUrl("http://www.ctrip.com", "get", true, 0, 0, header);

    usleep(200000);

    std::list<WebElement> collection;
	collection = page->getElementsByCssSelector("#cui_nav_ticket > a > span");
    std::list<WebElement>::iterator it;
    for(it = collection.begin(); it != collection.end(); it ++) {
        std::cout << it->getInnerXml() << std::endl;
        if ((*it).getInnerXml() == "门票") {
            std::cout << "点击门票" << std::endl;
            page->click((*it), 50000);
            break;
        }
    }

    int times = 0;
    int count = 0;

    while(count == 0) {
        collection = page->getElementsByCssSelector("#SearchNotice");
        count = collection.size();
        if (times > 10) {
            wk->destoryWebPage(page);
            return;
        }
        times ++;
        usleep(100000);
        std::cout << count << std::endl;
    }

    WebElement searchElement = page->getElementByCssSelector("#iptTicketSearchBtn");

    //WebElement tempElement = page->getElementByCssSelector("#ticketTab > div.s_links > p:nth-child(2) > a:nth-child(5)");
    //page->click(tempElement, 50000);

    std::cout << searchElement.getOuterXml() << std::endl;

    WebElement inputElement = collection.front();
    std::cout << inputElement.getInnerXml() << std::endl;
    std::string place;
    std::cin >> place;
    inputElement.type(place);
    std::cout << page->activeElement().getOuterXml() << std::endl;
    inputElement.sendKeys("keypress", Key_Enter);

    count = 0;
    std::cout << "try get info." << std::endl;
    while (count == 0) {
        std::cout << "." << std::endl;
        usleep(500000);
        collection = page->getElementsByCssSelector(".search_ticket_title");
        if (times > 10)
            break;
        times ++;
        count = collection.size();
    }

    for(it = collection.begin(); it != collection.end(); it ++)
        std::cout << it->getInnerXml() << std::endl;


    wk->destoryWebPage(page);
}
